package com.maxjay.system.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.maxjay.common.util.collection.CollectionUtil;
import com.maxjay.system.dao.ResourceDao;
import com.maxjay.system.model.Pair;
import com.maxjay.system.po.TSysResource;
import com.maxjay.system.service.ResourceService;

@SuppressWarnings({"rawtypes", "unchecked"})
@Service("resourceService")
@Transactional
public class ResourceServiceImpl extends BaseServiceImpl implements	ResourceService {
	
	/** 日志记录器 */
	private static Logger LOG = Logger.getLogger(ResourceServiceImpl.class);
	
	@Resource
	private ResourceDao resourceDao;
	
	private List<Pair<String, Collection<ConfigAttribute>>> resourceAuthoritiesList = null;

    /**
     * 获取资源URL路径和相应角色的集合
     * 
     * @return List<Pair<资源URL路径, 拥有该URL资源的角色集合>>
     */
	private List<Pair<String, Collection<String>>> loadUrlAuthorities() {
		List<Pair<String, Collection<String>>> list = new ArrayList<Pair<String, Collection<String>>>();
		DetachedCriteria dc = DetachedCriteria.forClass(TSysResource.class).addOrder(Order.desc("priority"));
		List<TSysResource> resources = this.baseDao.findByCriteria(dc);
		if(CollectionUtil.isNotEmpty(resources)){
			for(TSysResource resource : resources){
				Set<String> roleIds = resourceDao.getResourceRoles(resource.getObjId());
				Pair<String, Collection<String>> pair = new Pair(resource.getValue(), roleIds);
				list.add(pair);
			}
		}
		return list;
	}
	
	

	@Override
	public void loadResourceAuthoritiesList() {
		LOG.info("初始化加载SpringSecurity Url Resource资源配置开始...");
		resourceAuthoritiesList = new ArrayList<Pair<String,Collection<ConfigAttribute>>>();
    	List<Pair<String, Collection<String>>> urlAuthorities = loadUrlAuthorities();
    	if(CollectionUtil.isNotEmpty(urlAuthorities)){
	    	for(Pair<String, Collection<String>> pair : urlAuthorities){
	    		Collection<ConfigAttribute> configAttrs = null;
	    		if(CollectionUtil.isNotEmpty(pair.getSecond())){
	    			configAttrs = new HashSet<ConfigAttribute>();
		    		for(String authority : pair.getSecond()){
		    			configAttrs.add(new SecurityConfig(authority));
		    		}
	    		}
	    		resourceAuthoritiesList.add(new Pair<String, Collection<ConfigAttribute>>(pair.getFirst(), configAttrs));
	    	}
    	}
    	LOG.info("加载SpringSecurity Url Resource资源文件ok");
	}



	@Override
	public List<Pair<String, Collection<ConfigAttribute>>> getResourceAuthoritiesList() {
		if (resourceAuthoritiesList == null) {
			loadResourceAuthoritiesList();
		}
		return resourceAuthoritiesList;
	}

}
